package com.example.myapplication.ui.home;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.pojo.Article;


import java.text.SimpleDateFormat;
import java.util.List;

//这个是初始化 Home页面的 Adapter
public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context context;
    private LayoutInflater layoutInflater;

    private String TAG="HomeRecyclerViewAdapter";
    /**
     * 设置监听事件
     */
    public interface ViewClickListenser{
        void onDelete(View view,int id,int position);
        void onModify(View view,int id);
    }
    public ViewClickListenser viewClickListenser;

    public void setViewClickListenser(ViewClickListenser viewClickListenser) {
        this.viewClickListenser = viewClickListenser;
    }

    private List<Article> list;

    public HomeRecyclerViewAdapter(Context context){
        this.context = context;
    }
    public HomeRecyclerViewAdapter(Context context, List<Article> list){
        this.context=context;
        this.list=list;
        layoutInflater=LayoutInflater.from(context);
    }

    //    初始化item布局
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view=layoutInflater.inflate(R.layout.item,parent,false);
        MyViewHolder myViewHolder=new MyViewHolder(view,viewClickListenser);
        return myViewHolder;
    }

    //    处理每一个item 的地方,绑定数据
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final MyViewHolder myViewHolder=(MyViewHolder)holder;
        Log.d(TAG, "onBindViewHolder: "+list.get(position).getDate().toString());
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time = ft.format(list.get(position).getDate());
        myViewHolder.time.setText(time);
        myViewHolder.information.setText(list.get(position).getInformation());
    }

    //    显示数量，因为我的布局文件就是三个，所以我返回san
    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView time,information;
        private ViewClickListenser viewClickListenser;
        private Button delete ,modify;

        public MyViewHolder(@NonNull View itemView,ViewClickListenser viewClickListenser) {
            super(itemView);
            time=itemView.findViewById(R.id.time);
            information=itemView.findViewById(R.id.information);
            delete=itemView.findViewById(R.id.delete);
            modify=itemView.findViewById(R.id.modify);
            delete.setOnClickListener(this);
            modify.setOnClickListener(this);
            this.viewClickListenser=viewClickListenser;
        }
        @Override
        public void onClick(View v) {
            if(v.getId()==delete.getId()){
                viewClickListenser.onDelete(v,list.get(getAdapterPosition()).getId(),getAdapterPosition());
            }else if(v.getId()==modify.getId()){
                viewClickListenser.onModify(v,list.get(getAdapterPosition()).getId());
            }
        }
    }

}